//
//  Extension+UIButton.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/23.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

extension UIButton {
    
    convenience init(title: String? = nil, titleColor: UIColor? = UIColor.blackColor(), font:CGFloat = 18, backgroundImgName: String? = nil, imgName: String? = nil, isHighlighted: Bool = false, isSizeToFit: Bool = false, target: AnyObject?, action: Selector) {
        self.init()
        
        if let _title = title {
            self.setTitle(_title, forState: .Normal)
            self.setTitleColor(titleColor, forState: .Normal)
            self.titleLabel?.font = UIFont.systemFontOfSize(font)
        }
        
        if let _backgroundImgName = backgroundImgName {
            if isHighlighted == true {
                self.setBackgroundImage(UIImage(named: "\(_backgroundImgName)_highlighted"), forState: .Highlighted)
            }
            self.setBackgroundImage(UIImage(named: _backgroundImgName), forState: .Normal)
        }
        
        if let _imgName = imgName {
            if isHighlighted == true {
                self.setImage(UIImage(named: "\(_imgName)_highlighted"), forState: .Highlighted)
            }
            self.setImage(UIImage(named: _imgName), forState: .Normal)
        }
        
        if isSizeToFit == true {
            self.sizeToFit()
        }
        
        self.addTarget(target, action: action, forControlEvents:
            .TouchUpInside)
    }
}